package com.goldplusgold.tradedelay.app.home.live.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.goldplusgold.live.bean.AnswerBean;
import com.goldplusgold.imageloader.lib.IShow;
import com.goldplusgold.tradedelay.app.R;
import com.tb.emoji.EmojiUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;



/**
 * Created by Administrator on 2017/7/17.
 */

public class AnswerAdapter extends RecyclerView.Adapter<AnswerAdapter.ViewHolder> {

    List<AnswerBean> datas = new ArrayList<>();
    int offset = 0;
    int count = 10;
    Context context;

    public AnswerAdapter(Context context) {
        this.context = context;
    }

    @Override
    public AnswerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.live_all_ask_answer_item, parent, false);
        return new AnswerAdapter.ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(AnswerAdapter.ViewHolder holder, int position) {
        try {
            bindData(holder, position);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void bindData(AnswerAdapter.ViewHolder holder, final int position) throws IOException {
        if (datas.get(position).getAnswerBy() == null || "".equals(datas.get(position).getAnswerBy())) {
            holder.all_ask_answers_state.setVisibility(View.VISIBLE);
            holder.question_layout.setVisibility(View.GONE);
            IShow.getInstance().showCircle(context, datas.get(position).getAskImg(), R.drawable.head_image_default ,holder.questions_icon);
            if (datas.get(position).getAskContent().contains("[")) {
                EmojiUtil.handlerEmojiText(holder.questions_content, "       " + datas.get(position).getAskContent(), context);
            } else
                holder.questions_content.setText("       " + datas.get(position).getAskContent());
            holder.questions_name.setText(datas.get(position).getAskBy());
            holder.questions_time.setText(datas.get(position).getAskTime());
        } else {
            holder.all_ask_answers_state.setVisibility(View.GONE);
            holder.question_layout.setVisibility(View.VISIBLE);
            IShow.getInstance().showCircle(context, datas.get(position).getAskImg(), R.drawable.head_image_default,holder.answer_icon);
            if (datas.get(position).getAskContent().contains("[")) {
                EmojiUtil.handlerEmojiText(holder.answer_content, "       " + datas.get(position).getAskContent(), context);
            } else
                holder.answer_content.setText("       " + datas.get(position).getAskContent());
            holder.answer_time.setText(datas.get(position).getAskTime());
            holder.answer_name.setText(datas.get(position).getAskBy());
            IShow.getInstance().showCircle(context, datas.get(position).getAnswerImg(), R.drawable.head_image_default,holder.questions_icon);
            if (datas.get(position).getAnswerContent().contains("[")) {
                EmojiUtil.handlerEmojiText(holder.questions_content, "       " + datas.get(position).getAnswerContent(), context);
            } else
                holder.questions_content.setText("       " + datas.get(position).getAnswerContent());
            holder.questions_name.setText(datas.get(position).getAnswerBy());
            holder.questions_time.setText(datas.get(position).getAnswerTime());
        }
    }

    @Override
    public int getItemCount() {
        if (datas != null) {
            return datas.size();
        }
        return 0;
    }

    public void setDatas(List<AnswerBean> datas) {
        this.datas.clear();
        if (datas != null) {
            this.datas.addAll(datas);
            notifyChange();
        }
    }

    public void addDatas(List<AnswerBean> datas) {
        if (datas != null) {
            this.datas.addAll(datas);
            notifyChange();
        }
    }

    public void notifyChange() {
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView questions_icon;
        public TextView questions_name;
        public TextView questions_time;
        public TextView questions_content;
        public ImageView answer_icon;
        public TextView answer_name;
        public TextView answer_time;
        public TextView answer_content;
        public LinearLayout question_layout;
        public TextView all_ask_answers_state;
        private View mView;

        public ViewHolder(View view) {
            super(view);
            this.mView = view;
            question_layout = (LinearLayout) mView.findViewById(R.id.question_layout);
            all_ask_answers_state = (TextView) mView.findViewById(R.id.all_ask_answers_state);
            questions_icon = (ImageView) mView.findViewById(R.id.questions_icon);
            questions_name = (TextView) mView.findViewById(R.id.questions_name);
            questions_time = (TextView) mView.findViewById(R.id.questions_time);
            questions_content = (TextView) mView.findViewById(R.id.questions_content);
            answer_icon = (ImageView) mView.findViewById(R.id.answer_icon);
            answer_name = (TextView) mView.findViewById(R.id.answer_name);
            answer_time = (TextView) mView.findViewById(R.id.answer_time);
            answer_content = (TextView) mView.findViewById(R.id.answer_content);
        }
    }

}
